package com.huike.clues.demo;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Test {


    public List<Student> getData() {
        List<Student> lists = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            Student student = new Student();
            student.setId(i + 1);
            student.setName("李四" + i);
            student.setBirthday(new Date());
            student.setSalary(1500.00D);
            lists.add(student);
        }
        return lists;
    }

    //写出
    @org.junit.Test
   public void contextLoads() {
        EasyExcel.write("学生信息表.xlsx", Student.class).sheet().doWrite(getData());
    }

    //读取文件
    @org.junit.Test
    public void readExcel() throws Exception {

        List<Student_Read> list =  new ArrayList<>();

        /*
         * EasyExcel 读取 是基于SAX方式
         * 因此在解析时需要传入监听器
         */
        // 第一个参数 为 excel文件路径
        // 读取时的数据类型
        // 监听器
        FileInputStream ins = new FileInputStream(new File("C:\\Users\\ai'jie\\Desktop\\测试文件.xlsx"));
        EasyExcel.read(ins, Student_Read.class, new AnalysisEventListener<Student_Read>() {
            // 每读取一行就调用该方法
            @Override
            public void invoke(Student_Read data, AnalysisContext context) {
                list.add(data);
            }
            // 全部读取完成就调用该方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("读取完成");
            }
        }).sheet().doRead();
        list.forEach(System.out::println);
    }

}
